﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class InventoryRepository : RepositoryBase<Inventory>
    {
        public IQueryable<InventoryView> GetInventoryList(int categoryid, int storageId, string name)
        {
            if (storageId == 0)
            {
                var result = from pro in dbcontext.Products
                             join inv in dbcontext.Inventories.AsQueryable().Where(a => storageId == 0 || a.StorageId == storageId)
                             on pro.Id equals inv.ProuctId
                             into t
                             from x in t.DefaultIfEmpty()
                                 //join s in dbcontext.Storage
                                 //on t.StorageId equals s.Id
                             join cat in dbcontext.Categorys
                             on pro.CategoryId equals cat.Id
                             where (categoryid == 0 || pro.CategoryId == categoryid) && (name == "" || pro.Name.Contains(name) || pro.Number.Contains(name))
                             orderby pro.CreateTime descending
                             select new InventoryView
                             {
                                 Id = x != null ? x.Id : Guid.Empty,
                                 ProuctId = pro.Id,
                                 StorageId = x != null ? x.StorageId : 0,
                                 //StorageName = x != null ? x.Name : "",
                                 //StorageId = s.Id,
                                 //StorageName = s.Name,
                                 Amount = x != null ? x.Amount : 0,
                                 CategoryName = cat.Name,
                                 Format = pro.Format,
                                 Number = pro.Number,
                                 Price = pro.Price,
                                 SizeLenth = pro.SizeLenth,
                                 SizeWidth = pro.SizeWidth,
                                 Unit = pro.Unit,
                                 ProuctName = pro.Name,
                             };
                return result;
            }
            else
            {
                var result = from pro in dbcontext.Products
                             join inv in dbcontext.Inventories.AsQueryable().Where(a=>a.StorageId == storageId)
                             on pro.Id equals inv.ProuctId
                             //into t
                             //from x in t.DefaultIfEmpty()
                             //join s in dbcontext.Storage
                             //on t.StorageId equals s.Id
                             join cat in dbcontext.Categorys
                             on pro.CategoryId equals cat.Id
                             where (categoryid == 0 || pro.CategoryId == categoryid) && (name == "" || pro.Name.Contains(name) || pro.Number.Contains(name))
                             orderby pro.CreateTime descending
                             select new InventoryView
                             {
                                 Id = inv.Id,
                                 ProuctId = pro.Id,
                                 StorageId = inv.StorageId,
                                 //StorageName = x != null ? x.Name : "",
                                 //StorageId = s.Id,
                                 //StorageName = s.Name,
                                 Amount = inv.Amount,
                                 Price = pro.Price,
                                 CategoryName = cat.Name,
                                 Format = pro.Format,
                                 Number = pro.Number,
                                 SizeLenth = pro.SizeLenth,
                                 SizeWidth = pro.SizeWidth,
                                 Unit = pro.Unit,
                                 ProuctName = pro.Name,
                             };
                return result;
            }

        }
    }
}
